import {request } from 'ice';
import {API_URL} from './ApiUrl';
import {Message} from '@alifd/next';

/**
 * 用户列表接口
 * @param {*} params 
 */
async function listPage(params) {
    try {
        // console.log('查询用户列表 API 打印参数：',params);
        const response = await request.post(API_URL.API_USER_LIST, params);
        if(response.status == 0){
          return response.data;
        }else{
          const msg = response.msg ? response.msg : "服务忙, 请稍后再试!"
          Message.error(msg)
        }
    } catch (error) {
      console.error(error);
    }
}
/**
 * 查询下拉列表
 */
async function optionList() {
  try {
      const response = await request.get(API_URL.API_USER_OPTION_LIST);
      if(response.status == 0){
        return response.data;
      }else{
        const msg = response.msg ? response.msg : "服务忙, 请稍后再试!"
        Message.error(msg)
      }
  } catch (error) {
    console.error(error);
  }
}
/**
 * 查询用户详情
 * @param {*} params 
 */
async function detail(params) {
  try {
      const response = await request.post(API_URL.API_USER_DETAIL, {id: params});
      if(response.status == 0){
        return response.data;
      }else{
        const msg = response.msg ? response.msg : "服务忙, 请稍后再试!"
        Message.error(msg)
      }
  } catch (error) {
    console.error(error);
  }
}

/**
 * 新增用户
 * @param {*} params 
 */
async function add(params) {
  try {
      const response = await request.post(API_URL.API_USER_ADD, params);
      if(response.status == 0){
        return true;
      }else{
        const msg = response.msg ? response.msg : "服务忙, 请稍后再试!"
        Message.error(msg)
        return false;
      }
  } catch (error) {
    console.error(error);
  }
  return false;
}

/**
 * 修改用户信息
 * @param {*} params 
 */
async function update(params) {
  try {
      const response = await request.post(API_URL.API_USER_UPDATE, params);
      if(response.status == 0){
        return true;
      }else{
        const msg = response.msg ? response.msg : "服务忙, 请稍后再试!"
        Message.error(msg)
        return false;
      }
  } catch (error) {
    console.error(error);
  }
  return false;
}

/**
 * 删除用户
 * @param {*} params 
 */
async function deleteUser(params) {
  try {
      const response = await request.post(API_URL.API_USER_DELETE, {id: params});
      if(response.status == 0){
        return true;
      }else{
        const msg = response.msg ? response.msg : "服务忙, 请稍后再试!"
        Message.error(msg)
        return false;
      }
  } catch (error) {
    console.error(error);
  }
  return false;
}
/**
 * 启用用户
 * @param {*} params 
 */
async function enable(params) {
  const enableData = {id: params}
  try {
      const response = await request.post(API_URL.API_USER_ENABLE, enableData);
      if(response.status == 0){
        return true;
      }else{
        const msg = response.msg ? response.msg : "服务忙, 请稍后再试!"
        Message.error(msg)
        return false;
      }
  } catch (error) {
    console.error(error);
  }
  return false;
}

/**
 * 禁用用户
 * @param {*} params 
 */
async function disable(params) {
  try {
      const response = await request.post(API_URL.API_USER_DISABLE, {id: params});
      if(response.status == 0){
        return true;
      }else{
        const msg = response.msg ? response.msg : "服务忙, 请稍后再试!"
        Message.error(msg)
        return false;
      }
  } catch (error) {
    console.error(error);
  }
  return false;
}
/**
 * 部分代码相同度较高, 暂时不做重构
 * 2020-06-14
 */
export {listPage, optionList, detail, add, update, deleteUser, enable, disable}